<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../res/layui/css/layui.css"  media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
  <legend>增加和修改</legend>
</fieldset>
 
<form class="layui-form" action="" lay-filter="example">
	<input type='hidden' name='moduleId' id='moduleId'>
  <div class="layui-form-item">
    <label class="layui-form-label">上级模块名</label>
    <div class="layui-input-block">
      <input type="text" name="modModuleId" id="modModuleId" lay-filter="modModuleId" autocomplete="off" placeholder="请输入标题" class="layui-input" id="modmoduleId">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">模块名</label>
    <div class="layui-input-block">
      <input type="text" name="moduleName" lay-verify="title" id="moduleName" placeholder="请输入模块名" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">模块地址</label>
    <div class="layui-input-block">
      <input type="text" name="moduleAddress" id="moduleAddress" lay-verify="title" autocomplete="off" placeholder="请输入模块地址" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">是否有效</label>
    <div class="layui-input-block">
      <input type="checkbox" name="moduleValidity" checked="" id="moduleValidity" lay-skin="switch" lay-text="有效|无效">
    </div>
  </div>
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">备注</label>
    <div class="layui-input-block">
      <textarea placeholder="请输入备注信息" class="layui-textarea" lay-verify="title" name="moduleNote" id="moduleNote"></textarea>
    </div>
  </div> 
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
    </div>
  </div>
   
</form>
<script src="../res/layui/layui.js" charset="utf-8"></script>
<script src="../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../res/js/tools.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
layui.config({
    base: "../res/module/"
}).extend({
    treeSelect: "treeSelect/treeSelect"
}).use(['treeSelect','form', 'layedit', 'laydate','upload'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate
  ,upload = layui.upload
  ,treeSelect= layui.treeSelect;
  
  treeSelect.render({
      // 选择器
      elem: '#modModuleId',
      // 数据
      data: '../Module/findByAll.action',
      // 异步加载方式：get/post，默认get
      type: 'get',
      // 占位符
      placeholder: '请选择上级模块，不选择为一级',
      // 是否开启搜索功能：true/false，默认false
      search: false,
      style: {
          folder: {
              enable: false
          },
          line: {
              enable: true
          }
      },
      // 点击回调
      click: function(d){
          console.log(d);
      },
      // 加载完成后的回调函数
      success: function (d) {
          console.log(d);
//          选中节点，根据id筛选
         
//          获取zTree对象，可以调用zTree方法
          var treeObj = treeSelect.zTree('modModuleId');
          console.log(treeObj);

//          刷新树结构
          treeSelect.refresh('modModuleId');
      }
  });
  
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 2){
        return '标题至少得2个字符啊';
      }
    }
    ,pass: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位，且不能出现空格'
    ]
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  //监听提交
  form.on('submit(demo1)', function(data){
    var url = "../Module/insert.action";
    $.post(url,data.field,function(obj){
    	if(obj.state==1){
    		layer.msg(obj.msg,{
				icon:1,
			 	time:1000,
			 },function(){
				 var indexs = parent.layer.getFrameIndex(window.name);
				 parent.layer.close(indexs);
			 });
    		return false ;
    	}
    },"json")
    return false;
  });
  //表单赋值
  layui.$('#LAY-component-form-setval').on('click', function(){
    form.val('example', {
      "username": "贤心" // "name": "value"
      ,"password": "123456"
      ,"interest": 1
      ,"like[write]": true //复选框选中状态
      ,"close": true //开关状态
      ,"sex": "女"
      ,"desc": "我爱 layui"
    });
  });
  //表单取值
  layui.$('#LAY-component-form-getval').on('click', function(){
    var data = form.val('example');
    alert(JSON.stringify(data));
  });
  var id = getQueryString("id");
  if(id!=null){
	  var url = "../Module/findId.action";
		var data = {moduleId:id};
		$.post(url,data,function(obj){
			if(obj.modModuleId!=0){
				treeSelect.checkNode('modModuleId', obj.modModuleId);
				form.render();
			}
			$("#moduleId").val(obj.moduleId);
			$("#moduleName").val(obj.moduleName);
			$("#moduleAddress").val(obj.moduleAddress);
			$("#moduleNote").val(obj.moduleNote);
			if(obj.moduleValidity=="有效"){
				 form.val('example', {
					"moduleValidity": true //开关状态
				})
			}else{
				form.val('example', {
				    "moduleValidity": false//开关状态
				})
			}
		},'json')
  }
});
</script>
</body>
</html>